package com.ibix.netty.communion;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.CharsetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * Created by Administrator on 2018/1/23.
 */
public class MyClientInitializer extends ChannelInitializer<SocketChannel> {
    protected static final Logger LOGGER = LoggerFactory.getLogger(MessageHandle.class);

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();
        //客户端超时时间设置

        pipeline.addLast(new IdleStateHandler(15, 5, 0));

        ByteBuf delimiter = Unpooled.copiedBuffer("<RE>".getBytes());
        pipeline.addLast("framer", new DelimiterBasedFrameDecoder(Integer.MAX_VALUE, delimiter));

        pipeline.addLast(new ObjectDecoder(1024 * 1024, ClassResolvers.cacheDisabled(this.getClass().getClassLoader())));
        pipeline.addLast(new ObjectEncoder());


        pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
        pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
        pipeline.addLast("handler", new MyClientHandler(LOGGER));
    }
}
